package com.sx.manage.util;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.sx.manage.biz.consumer.relation.IMConsumerDataRelationBiz;
import com.sx.manage.service.sys.IMBaseSystemConfigInfoService;
import com.sx.model.constants.MSystemConstants;
import com.sx.model.constants.type.consumer.MConsumerDataRelationType;
import com.sx.model.manage.mybatis.form.consumer.MConsumerDataRelationForm;
import com.sx.model.manage.mybatis.form.sys.MSystemConfigInfoForm;
import com.sx.model.manage.mybatis.vo.consumer.MConsumerDataRelationVO;
import com.sx.model.manage.mybatis.vo.result.ResultBooleanVO;
import com.sx.model.manage.mybatis.vo.sys.MSysConfigVO;



/**
 *  service impl 辅助类
 * @author lenovo
 *
 */
@Service("webServiceUtils")
public class WebServiceUtils {
	// 日志
	private  Logger _log = LoggerFactory.getLogger(WebServiceUtils.class);
	// 用户关系数据
	@Resource(name = "mConsumerDataRelationBizImpl")
	private IMConsumerDataRelationBiz mConsumerDataRelationBizImpl;
	//系统参数配置
	@Resource(name="mBaseSystemConfigInfoServiceImpl")
	private IMBaseSystemConfigInfoService mBaseSystemConfigInfoServiceImpl;
	
	public IMConsumerDataRelationBiz getmConsumerDataRelationBizImpl() {
		return mConsumerDataRelationBizImpl;
	}

	public void setmConsumerDataRelationBizImpl(IMConsumerDataRelationBiz mConsumerDataRelationBizImpl) {
		this.mConsumerDataRelationBizImpl = mConsumerDataRelationBizImpl;
	}

	/** 判断个人是否开通交易会员代码 **/
	public ResultBooleanVO isOpeanAccountHX02(Long consumer_id) {

		MConsumerDataRelationVO relationVO = mConsumerDataRelationBizImpl.query_by_consumer_id_and_relation_type(consumer_id, MConsumerDataRelationType.PERSON_MEMBER_CODE_HUAXIA_02).getObjVo();
		if (null != relationVO) {
			return new ResultBooleanVO();
		} else {
			return new ResultBooleanVO(false, null, null);
		}
	}

	/** 获取用户华夏交易会员代码 **/
	public String queryTradeMemberCode(Long consumer_id) {
		_log.info("获取用户交易会员代码consumer_id={}",consumer_id);
		MConsumerDataRelationVO relationVO = mConsumerDataRelationBizImpl.query_by_consumer_id_and_relation_type(consumer_id, MConsumerDataRelationType.PERSON_MEMBER_CODE_HUAXIA_02).getObjVo();
		if (null != relationVO) {
			return relationVO.getRelation_string_num();
		}
		return null;
	}

	/** 获取用户二期摊位号 **/
	public String queryAccountNo(Long consumer_id) {
		_log.info("获取用户二期摊位号consumer_id={}",consumer_id);
		MConsumerDataRelationVO relationVO = mConsumerDataRelationBizImpl.query_by_consumer_id_and_relation_type(consumer_id, MConsumerDataRelationType.P_ACCOUNT_CODE_02).getObjVo();
		if (null != relationVO) {
			return relationVO.getRelation_string_num();
		}
		return null;
	}
	/** 获取用户华夏子账号 **/
	public String queryBranchAccountNo(Long consumer_id) {
		_log.info("获取用户华夏子账号consumer_id={}",consumer_id);
		MConsumerDataRelationVO relationVO = mConsumerDataRelationBizImpl.query_by_consumer_id_and_relation_type(consumer_id, MConsumerDataRelationType.P_BANK_ACCOUNT_CODE_02).getObjVo();
		if (null != relationVO) {
			return relationVO.getRelation_string_num();
		}
		return null;
	}

	/** 获取用户二期摊位号 **/
	public String queryAccountNoCompany(Long companyId) {
		_log.info("获取用户二期摊位号companyId={}",companyId);
		MConsumerDataRelationVO relationVO = mConsumerDataRelationBizImpl.query_by_consumer_id_and_relation_type(companyId, MConsumerDataRelationType.C_ACCOUNT_CODE_02).getObjVo();
		if (null != relationVO) {
			return relationVO.getRelation_string_num();
		}
		return null;
	}
	/** 获取用户华夏交易会员代码 **/
	public String queryTradeMemberCodeCompany(Long companyId) {
		_log.info("获取用户交易会员代码companyId={}",companyId);
		MConsumerDataRelationVO relationVO = mConsumerDataRelationBizImpl.query_by_consumer_id_and_relation_type(companyId, MConsumerDataRelationType.COMPANY_MEMBER_CODE_HUAXIA_02).getObjVo();
		if (null != relationVO) {
			return relationVO.getRelation_string_num();
		}
		return null;
	}

	/** 获取用户华夏子账号 **/
	public String queryBranchAccountNoCompany(Long companyId) {
		_log.info("获取用户华夏子账号companyId={}",companyId);
		MConsumerDataRelationVO relationVO = mConsumerDataRelationBizImpl.query_by_consumer_id_and_relation_type(companyId, MConsumerDataRelationType.C_BANK_ACCOUNT_CODE_02).getObjVo();
		if (null != relationVO) {
			return relationVO.getRelation_string_num();
		}
		return null;
	}
	/** 根据子账号获取公司id **/
	public Long queryCompanyIdByBankAccountNo(String bankAccountNo) {
		_log.info("根据子账号获取公司id bankAccountNo={}",bankAccountNo);
		MConsumerDataRelationForm form = new MConsumerDataRelationForm();
		form.setRelation_string_num(bankAccountNo);
		form.setRelation_type(MConsumerDataRelationType.C_BANK_ACCOUNT_CODE_02);
		List<MConsumerDataRelationVO> relationVOList = mConsumerDataRelationBizImpl.queryList(form).getData();
		if(relationVOList.size()==1){
			return relationVOList.get(0).getConsumer_id();
		}
		return null;
	}
	/** 根据子账号获取公司id **/
	public Long queryCompanyIdByTradeMemCode(String tradeMemCode) {
		_log.info("根据子账号获取公司id tradeMemCode={}",tradeMemCode);
		MConsumerDataRelationForm form = new MConsumerDataRelationForm();
		form.setRelation_string_num(tradeMemCode);
		form.setRelation_type(MConsumerDataRelationType.COMPANY_MEMBER_CODE_HUAXIA_02);
		List<MConsumerDataRelationVO> relationVOList = mConsumerDataRelationBizImpl.queryList(form).getData();
		if(relationVOList.size()==1){
			return relationVOList.get(0).getConsumer_id();
		}
		return null;
	}
	/** 根据子账号获取用户id **/
	public Long queryConsumerIdByBankAccountNo(String bankAccountNo) {
		_log.info("根据子账号获取公司id bankAccountNo={}",bankAccountNo);
		MConsumerDataRelationForm form = new MConsumerDataRelationForm();
		form.setRelation_string_num(bankAccountNo);
		form.setRelation_type(MConsumerDataRelationType.P_BANK_ACCOUNT_CODE_02);
		List<MConsumerDataRelationVO> relationVOList = mConsumerDataRelationBizImpl.queryList(form).getData();
		if(relationVOList.size()==1){
			return relationVOList.get(0).getConsumer_id();
		}
		return null;
	}
	/** 根据子账号获取公司id **/
	public Long queryConsumerIdByTradeMemCode(String tradeMemCode) {
		_log.info("根据子账号获取公司id tradeMemCode={}",tradeMemCode);
		MConsumerDataRelationForm form = new MConsumerDataRelationForm();
		form.setRelation_string_num(tradeMemCode);
		form.setRelation_type(MConsumerDataRelationType.PERSON_MEMBER_CODE_HUAXIA_02);
		List<MConsumerDataRelationVO> relationVOList = mConsumerDataRelationBizImpl.queryList(form).getData();
		if(relationVOList.size()==1){
			return relationVOList.get(0).getConsumer_id();
		}
		return null;
	}
	/**
	 * 获取系统默认的 渠道类型
	 * @return
	 */
	public String defaultChannelType() {
		MSystemConfigInfoForm form = new MSystemConfigInfoForm();
		form.setConfig_name(MSystemConstants.CHANNEL_TYPE_DEFAULT_USED);
		MSysConfigVO configVO = mBaseSystemConfigInfoServiceImpl.queryByIdSystemConfig(form).getObjVo();
		if( null != configVO) {
			return configVO.getConfig_value();
		}
		return null;
	}
	

	/** 获取用户默认使用开户渠道 **/
	public String queryConsumerChannelType(Long consumer_id) {
		_log.info("获取用户默认渠道consumer_id={}");
		MConsumerDataRelationVO relationVO = mConsumerDataRelationBizImpl.query_by_consumer_id_and_relation_type(consumer_id, MConsumerDataRelationType.CONSUMER_PERSON_CHANNEL_TYPE).getObjVo();
		if (null == relationVO) {
			_log.info("用户没有可以使用的渠道consumer_id={}", consumer_id);
			return null;
		}
		return relationVO.getRelation_string_num();
	}
}

